﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using Thor.Common;
using Thor.Domain;

namespace Thor.Domain.Configuration
{
    /// <summary>
    /// Represents an individual with rights to administer
    /// the Thor services.
    /// </summary>
    public class AdministratorAccount : ThorDomainObjectBase, IAdministratorAccount
    {
        #region Constants

        private const string EMAILADDRESS = "EmailAddress";
        private const string FIRSTNAME = "FirstName";
        private const string LASTNAME = "LastName";
        private const string USERNAME = "UserName";
        private const string ID = "Id";

        #endregion

        #region Fields

        private string _emailAddress = string.Empty;
        private string _firstName = string.Empty;
        private string _lastName = string.Empty;
        private string _userName = string.Empty;
        private Guid _id = Guid.Empty;
        private AdministratorAccount _cache = null;

        #endregion

        #region Constructors

        public AdministratorAccount() : base() { }

        #endregion

        #region Properties

        /// <summary>
        /// Unique id of the administrator.
        /// </summary>
        [Key]
        public Guid Id
        {
            get { return _id; }
            set
            {
                if (_id != value)
                {
                    _id = value;
                    OnPropertyChanged(ID);
                }
            }
        }

        /// <summary>
        /// Email address of the administrator.
        /// </summary>
        [Required]
        public string EmailAddress 
        {
            get { return _emailAddress; }
            set
            {
                if (_emailAddress != value)
                {
                    _emailAddress = value;
                    OnPropertyChanged(EMAILADDRESS);
                }
            }
        }

        /// <summary>
        /// The administrators first name.
        /// </summary>
        [Required]
        public string FirstName
        {
            get { return _firstName; }
            set
            {
                if (_firstName != value)
                {
                    _firstName = value;
                    OnPropertyChanged(FIRSTNAME);
                }
            }
        }

        /// <summary>
        /// The administrator's last name.
        /// </summary>
        [Required]
        public string LastName
        {
            get { return _lastName; }
            set
            {
                if (_lastName != value)
                {
                    _lastName = value;
                    OnPropertyChanged(LASTNAME);
                }
            }
        }

        [Required]
        public string UserName
        {
            get { return _userName; }
            set
            {
                if (_userName != value)
                {
                    _userName = value;
                    OnPropertyChanged(USERNAME);
                }
            }
        }

        #endregion

        #region Methods

        public override void BeginEdit()
        {
            _cache = new AdministratorAccount();
            _cache.EmailAddress = this.EmailAddress;
            _cache.FirstName = this.FirstName;
            _cache.LastName = this.LastName;
            _cache.UserName = this.UserName;
            _cache.Id = this.Id;
        }

        public override void CancelEdit()
        {
            this.EmailAddress = _cache.EmailAddress;
            this.FirstName = _cache.FirstName;
            this.LastName = _cache.LastName;
            this.UserName = _cache.UserName;
            this.Id = _cache.Id;
        }

        public override void EndEdit()
        {
            _cache = null;
        }

        #endregion
    }
}
